<?php
/**
 * bs多规格sku商城系统
 * ============================================================================
 * 版权所有 2024-2099 wltx666.com，并保留所有权利。
 * 网站地址: https://wltx666.com
 * ============================================================================
 * Author: 草上飞
 * Email: wltx@alipay168.cn
 * QQ: 2719875001
 * Date: 2025/05/15
 * 描述: api_xxx 代表前端接口,其它代表后端接口
 */

namespace app\result\auto;

use app\model\auto\AdminMsg;
use app\result\BaseResult;
use GatewayClient\Gateway;


class AdminMsgResult extends BaseResult
{
    public function __construct()
    {
        parent::__construct();
        $this->model = new AdminMsg();
    }

    // 表格列
    public function el_table_column(): array
    {
        return [
            ['prop' => 'id', 'type' => 'text', 'label' => '序号', 'minWidth' => '120', 'align' => 'center',],
            ['prop' => 'title', 'type' => 'text', 'label' => '名称', 'minWidth' => '120', 'align' => 'center',],
            ['prop' => 'content', 'type' => 'text', 'label' => '内容', 'minWidth' => '120', 'align' => 'center',],
            ['prop' => 'link', 'type' => 'text', 'label' => '跳转地址', 'minWidth' => '120', 'align' => 'center',],
            ['prop' => 'is_read', 'type' => 'tags', 'label' => '是否已读', 'minWidth' => '120', 'align' => 'center',
                'tags' => [
                    1 => ['label' => '已读', 'color' => '', 'type' => 'success',],
                    0 => ['label' => '未读', 'color' => '', 'type' => 'error',],
                ]],
            ['prop' => 'created_at', 'type' => 'text', 'label' => '添加时间', 'minWidth' => '120', 'align' => 'center',],
            ['prop' => 'updated_at', 'type' => 'text', 'label' => '更新时间', 'minWidth' => '120', 'align' => 'center',],
        ];
    }

    //列表搜索字段
    private function _searchField(): array
    {
        return [
            ['name' => 'title', 'label' => '名称', 'type' => 'input', 'placeholder' => '消息名称',],
            ['name' => 'content', 'label' => '内容', 'type' => 'input', 'placeholder' => '消息内容',],
        ];
    }

    // 获取公共条件限制
    private function _where($args = []): array
    {
        $where = [];
        $keyword = trim($args['keyword'] ?? '');
        $status = $args['status'] ?? '';
        $title = trim($args['title'] ?? '');
        $content = trim($args['content'] ?? '');
        if ($keyword) {
            $where[] = ['title', 'like', '%' . trim($args['keyword']) . '%'];
        }
        if ($title) {
            $where[] = ['title', 'like', '%' . trim($args['title']) . '%'];
        }
        if ($content) {
            $where[] = ['content', 'like', '%' . trim($args['content']) . '%'];
        }

        if ($status) {
            $where[] = ['status', '=', $status];
        }
        return $where;
    }

    public function list($args = []): array
    {
        $page = intval($args['page'] ?? 1);
        $pageSize = intval($args['limit'] ?? 10);
        $sort_by = trim($args['sort_by'] ?? 'id desc');
        $where = $this->_where($args);
        $total = $this->model->data_count($where);
        $data = $this->model->data_list($where, '*', $sort_by, $page, $pageSize);
        return $this->success_return('success', [
            'total' => $total,
            'list' => $data,
            'el_table_column' => $this->el_table_column(),
            'search_fields' => $this->_searchField()
        ]);
    }


    // 删除数据
    public function del_data($args): array
    {
        $ids = $args['ids'] ?? [];
        if (!$ids) {
            $ids = !empty($args['id']) ? [$args['id']] : [];
        }
        if (!$ids) {
            return $this->error_return('请选择数据');
        }
        $data = $this->model->data_list_nopage([["id", 'in', $ids]], '*');
        if (!$data) {
            return $this->error_return('数据不存在');
        }
        //判断是否存在del_time
        if (isset($data[0]['del_time'])) {
            $res = $this->model->data_update([["id", 'in', $ids]], ['del_time' => time()]);
        } else {
            $res = $this->model->data_del([["id", 'in', $ids]]);
        }
        if (!$res) {
            return $this->error_return('删除失败');
        }
        admin_log('删除客服数据', $data);
        return $this->success_return('删除成功');
    }


    public static function push_msg($args): array
    {
        $title = trim($args['title'] ?? '');
        $content = trim($args['content'] ?? '');
        $link = trim($args['link'] ?? '');
        if (!$title) {
            return self::error_return('请输入标题');
        }
        if (!$content) {
            return self::error_return('请输入内容');
        }
        $data = [
            'title' => $title,
            'content' => $content,
            'link' => $link,
            'status' => 0,
        ];
        $res = (new AdminMsg())->data_add($data);
        if (!$res) {
            return self::error_return('添加失败');
        }

        //推送给所有管理员
        Gateway::sendToGroup('admin_msg_group', json_encode([
            'type' => 'msg',
            'title' => $title,
            'content' => $content,
            'duration' => 1000,
            'notify_type' => 'success',
            'link' => $link
        ]));
        bs_log(Gateway::getClientSessionsByGroup('admin_msg_group'), 'data', '推送消息给管理员.log');

        return self::success_return('添加成功');
    }

}
